<?php

/**
 * @author      :   QuangLT
 * @name        :   Thethao_Business_Block
 * @copyright   :   Fpt Online
 * @todo        :   Block business
 */
class Fpt_Business_News_Adapter_MySql extends Fpt_Business_News_Adapter_Abstract
{

    protected static $_instance = null;

    /**
     * Constructor of class
     * we don't permit an explicit call of the constructor! (like $v = new Singleton())
     */
    public function __construct()
    {
        
    }

    /**
     * Get singletom instance
     * @return <object>
     */
    public final static function getInstance()
    {
        //Check instance
        if (is_null(self::$_instance))
        {
            self::$_instance = new self();
        }

        //Return instance
        return self::$_instance;
    }
    
    
    /**
     * Get one latest news on each category
     * @param <int> $intArticleType
     * @param <string> $strListCateID
     * @param <int> $intLimit
     * @return <array> List article id 
     */
    public function getListArticleByTime($arrParams)
    {
        $arrResult = array();
        try
        {
            // Get DB
            $sport_m = Fpt_Global::getSportDB();
            //p_articletype INT, p_lstcategoryid VARCHAR(300), p_offset INT, p_limit INT, OUT p_rowcount INT)
            $stmt = $sport_m->prepare('CALL sp_getLastestArticle(:p_articletype, :p_lstcategoryid, :p_offset, :p_limit, @p_rowcount)');

            //Bind param
            $stmt->bindParam('p_articletype', $arrParams['ArticleType']);
            $stmt->bindParam('p_lstcategoryid', $arrParams['CateID'], PDO::PARAM_STR);
            $stmt->bindParam('p_offset', $arrParams['Offset'], PDO::PARAM_INT);
            $stmt->bindParam('p_limit', $arrParams['Limit'], PDO::PARAM_INT);
           
            // Fetch result
            $stmt->execute();
            $arrResult = $stmt->fetchAll();
            // Close cursor
            $stmt->closeCursor();
            
            $stmt = $sport_m->query("SELECT @p_rowcount");
            $foundRows = $stmt->fetchColumn();

            //Return array
            $arrResult["data"]     = $arrResult;
            $arrResult["total"]    = $foundRows;
            
            
            // Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrResult;
    }
    
    
    public function getListTopArticle($arrParams)
    {
        $arrResult = array('total' => 0, 'data' => array());
        try
        {
            //Get DB
            $sport_m = Fpt_Global::getSportDB();
            $stmt = $sport_m->prepare('CALL sp_getLastestArticle(:p_articletype, :p_lstcategoryid, :p_offset, :p_limit, @p_rowcount)');

            //Bind param
            $stmt->bindParam('p_articletype', $arrParams['article_type']);
            $stmt->bindParam('p_lstcategoryid', $arrParams['category_id'], PDO::PARAM_STR);
            $stmt->bindParam('p_offset', $arrParams['offset'], PDO::PARAM_INT);
            $stmt->bindParam('p_limit', $arrParams['limit'], PDO::PARAM_INT);
           
            // Fetch result
            $stmt->execute();
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
            {
                
                $arrResult['data'][] = $row['article_id'];
            }
            // Close cursor
            $stmt->closeCursor();
            
            $stmt = $sport_m->query("SELECT @p_rowcount");
            $foundRows = $stmt->fetchColumn();

            //Return array
            $arrResult["total"]    = $foundRows;

            // Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrResult;
    }
    
    
    
    public function getListArticleEachCateByTime($limit)
    {
        $arrResult = array();
        try
        {
            // Get DB
            $sport_m = Fpt_Global::getSportDB();
            //p_articletype INT, p_lstcategoryid VARCHAR(300), p_offset INT, p_limit INT, OUT p_rowcount INT)
            $stmt = $sport_m->prepare('CALL sp_getArticleEachCate(:p_limit)');
            
            //Bind param
            $stmt->bindParam('p_limit', $limit, PDO::PARAM_INT);
            
            // Fetch result
            $stmt->execute();
            
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
            {
                $arrResult[] = $row['article_id'];
            }

            // Close cursor
            $stmt->closeCursor();
            
            //Return array
            $arrResult["data"]     = $arrResult;
            // Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrResult;
    }
    
    /**
     * get latest news on cate (+subcates)
     * @param string $strCateID
     * @param int $offset
     * @param int $limit
     * @return array 
     * @author PhuongTN
     */
    public function getLatestNewsOnCate($strCateID, $offset, $limit)
    {
        $arrResult = array();
        try
        {
            // Get DB
            $sport_m = Fpt_Global::getSportDB();
            $stmt = $sport_m->prepare('CALL sp_getLatestNewsOnFolder(:p_cateid, :p_offset, :p_limit, @p_rowcount)');
            
            //Bind param
            $stmt->bindParam('p_cateid', $strCateID, PDO::PARAM_STR);
            $stmt->bindParam('p_offset', $offset, PDO::PARAM_INT);
            $stmt->bindParam('p_limit', $limit, PDO::PARAM_INT);

            // Fetch result
            $stmt->execute();
            $arrData = array();
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
            {
                $arrData[] = $row['article_id'];
            }

            // Close cursor
            $stmt->closeCursor();
            
            $stmt = $sport_m->query("SELECT @p_rowcount");
            $arrResult["total"]  = $stmt->fetchColumn();
            //Return array
            $arrResult["data"]     = $arrData;
            // Release
            unset($stmt);
            unset($arrData);
        }
        catch (Zend_Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrResult;
    }
    
    /**
     * get list other news on cate with publish_time < $fromDate order by publish_time
     * @param int $cateID
     * @param int $fromDate
     * @param int $offset
     * @param int $limit
     * @return array 
     * @author PhuongTN
     */
    public function getListOtherNews($cateID, $fromDate, $offset, $limit)
    {
        $arrResult = array();
        try
        {
            // Get DB
            $sport_m = Fpt_Global::getSportDB();
            $stmt = $sport_m->prepare('CALL sp_getOtherNews(:p_cateid, :p_fromdate, :p_offset, :p_limit, @p_rowcount)');
            
            //Bind param
            $stmt->bindParam('p_cateid', $cateID, PDO::PARAM_INT);
            $stmt->bindParam('p_fromdate', $fromDate, PDO::PARAM_INT);
            $stmt->bindParam('p_offset', $offset, PDO::PARAM_INT);
            $stmt->bindParam('p_limit', $limit, PDO::PARAM_INT);

            // Fetch result
            $stmt->execute();
            $arrData = array();
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
            {
                $arrData[] = $row['article_id'];
            }

            // Close cursor
            $stmt->closeCursor();
            
            $stmt = $sport_m->query("SELECT @p_rowcount");
            $arrResult["total"]  = $stmt->fetchColumn();
            //Return array
            $arrResult["data"]     = $arrData;
            // Release
            unset($stmt);
            unset($arrData);
        }
        catch (Zend_Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrResult;
    }
    
    /**
     * Get latest news on topic
     * @param array $arrParams array(ArticleType, TopicID, Offset, Limit)
     * @return <array> List article id 
     */
    public function getListArticleByTopic($arrParams)
    {
        $arrResult = array();
        try
        {
            // Get DB
            $sport_m = Fpt_Global::getSportDB();
            $stmt = $sport_m->prepare('CALL sp_getArticleByTopic(:p_topicid, :p_offset, :p_limit, @p_rowcount)');

            //Bind param
            /*$stmt->bindParam('p_articletype', $arrParams['ArticleType']);*/
            $stmt->bindParam('p_topicid', $arrParams['TopicID'], PDO::PARAM_STR);
            $stmt->bindParam('p_offset', $arrParams['Offset'], PDO::PARAM_INT);
            $stmt->bindParam('p_limit', $arrParams['Limit'], PDO::PARAM_INT);
           
            // Fetch result
            $stmt->execute();
            $arrResult = $stmt->fetchAll();
            // Close cursor
            $stmt->closeCursor();
            
            $stmt = $sport_m->query("SELECT @p_rowcount");
            $foundRows = $stmt->fetchColumn();

            //Return array
            $arrResult["data"]     = $arrResult;
            $arrResult["total"]    = $foundRows;
            

            // Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrResult;
    }

    /**
     * get all topic related by article Id
     * @param int $articleID
     * @param int $topicID default null, else get config(row, createtiontime) 
     * @return array 
     * @author PhuongTN
     */
    public function getTopicsByArticleID($articleID, $topicID = NULL)
    {
        $arrResult = array();
        try
        {
            // Get DB
            $sport_m = Fpt_Global::getSportDB();
            $stmt = $sport_m->prepare('CALL sp_getTopicsOfArticle(:p_articleid, :p_topicid)');

            //Bind param
            $stmt->bindParam('p_articleid', $articleID, PDO::PARAM_INT);
            
            if(empty($topicID))
            {
                $stmt->bindParam('p_topicid', $topicID, PDO::PARAM_NULL);
            }
            else
            {
                $stmt->bindParam('p_topicid', $topicID, PDO::PARAM_INT);
            }
            // Fetch result
            $stmt->execute();
            
            $arrResult = $stmt->fetchAll();
            // Close cursor
            $stmt->closeCursor();
            
            // Release
            unset($stmt);
            
            return $arrResult;
        }
        catch (Zend_Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrResult;
    }
    
    /**
     * get hot info related by article Id
     * @param int $articleID
     * @return array 
     * @author PhuongTN
     */
    public function getHotInfoByArticleID($articleID)
    {
        $arrResult = array();
        try
        {
            // Get DB
            $sport_m = Fpt_Global::getSportDB();
            $stmt = $sport_m->prepare('CALL sp_getHotNewsByArticle(:p_articleid)');

            //Bind param
            $stmt->bindParam('p_articleid',$articleID, PDO::PARAM_INT);
            
            // Fetch result
            $stmt->execute();
            
            $arrResult = $stmt->fetchAll();
            // Close cursor
            $stmt->closeCursor();
            
            return $arrResult;
            
            // Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrResult;
    }
    /**
     * get all cate related by article Id
     * @param int $articleID
     * @return array 
     * @author PhuongTN
     */
    public function getCategoriesByArticleID($articleID)
    {
        $arrResult = array();
        try
        {
            // Get DB
            $sport_m = Fpt_Global::getSportDB();
            $stmt = $sport_m->prepare('CALL sp_getCategoriesOfArticle(:p_articleid)');

            //Bind param
            $stmt->bindParam('p_articleid',$articleID, PDO::PARAM_INT);
            
            // Fetch result
            $stmt->execute();
            
            $arrResult = $stmt->fetchAll();
            // Close cursor
            $stmt->closeCursor();
            
            return $arrResult;
            
            // Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrResult;
    }
    
    /**
     * get all team related by article Id
     * @param int $articleID
     * @return array 
     * @author PhuongTN
     */
    public function getTeamsByArticleID($articleID)
    {
        $arrResult = array();
        try
        {
            // Get DB
            $sport_m = Fpt_Global::getSportDB();
            $stmt = $sport_m->prepare('CALL sp_getTeamsOfArticle(:p_articleid)');

            //Bind param
            $stmt->bindParam('p_articleid',$articleID, PDO::PARAM_INT);
            
            // Fetch result
            $stmt->execute();
            
            $arrResult = $stmt->fetchAll();
            // Close cursor
            $stmt->closeCursor();
            
            return $arrResult;
            
            // Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrResult;
    }
    
    /**
     * Get hot news
     * @param array $arrParams
     * @return array
     * @author  : phongtx@fpt.net
     */
    public function getHotNews($arrParams)
    {
        $arrResult = array();
        try
        {
            $sport_m = Fpt_Global::getSportDB();
            $stmt = $sport_m->prepare('CALL sp_getHotNews(:p_categoryid, :p_articletype, :p_offset, :p_limit, @p_result)');
            
			$intOffset = 0;
            //Bind param
            $stmt->bindParam('p_categoryid', $arrParams['category_id'], PDO::PARAM_INT);
            $stmt->bindParam('p_articletype', $arrParams['article_type'], PDO::PARAM_INT);
            $stmt->bindParam('p_offset', $intOffset, PDO::PARAM_INT);
            $stmt->bindParam('p_limit', $arrParams['limit'], PDO::PARAM_INT);

            //Fetch result
            $stmt->execute();
            $arrResultTmp = $stmt->fetchAll();
            
            if(!empty($arrResultTmp)){
                foreach($arrResultTmp as $arrItem){
                    $arrResult[] = $arrItem['article_id'];
                }
            }

            //Close cursor
            $stmt->closeCursor();

            //Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return false;
        }
        return $arrResult;
    }

    /**
     * Get Article Detail
     * @param int $articleID
     * @return array
     * @author  : phongtx@fpt.net
     */
    public function getDetailArticle($articleID)
    {
        $arrResult = array();
        try
        {
            $sport_m = Fpt_Global::getSportDB();

            $stmt = $sport_m->prepare('CALL sp_getDetailarticles(:p_article)');

            //Bind param
            $stmt->bindParam('p_article', $articleID, PDO::PARAM_STR);

            //Fetch result
            $stmt->execute();
            $arrResult = $stmt->fetch();
          
            //Close cursor
            $stmt->closeCursor();

            //Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return false;
        }
        return $arrResult;
    }

    /**
     * Get list article detail
     * @param array $arrParams
     * @return array
     * @author  : phongtx@fpt.net
     */
    public function getListArticle($arrParams)
    {
        try
        {
            $sport_m = Fpt_Global::getSportDB();
            $stmt = $sport_m->prepare('CALL sp_getDetailarticles(:p_article)');

            //Bind param
            $stmt->bindParam('p_article', $articleID, PDO::PARAM_INT);

            //Fetch result
            $stmt->execute();
            $arrResult = $stmt->fetchAll();

            //Close cursor
            $stmt->closeCursor();

            //Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return false;
        }
        return $arrResult;
    }

    /**
     * Function get list relative artilce
     * @param array $arrParams(article_id, article_type, limit, offset)
     * @return array
     * @author  : phongtx@fpt.net
     */
    public function getRelativeArticle($arrParams)
    {
        $arrResult = array();
        try
        {
            $sport_m = Fpt_Global::getSportDB();
            $stmt = $sport_m->prepare('CALL sp_getRelativeArticle(:p_articleid, :p_articletype, :p_limit);');

            //Bind param
            $stmt->bindParam('p_articleid', $arrParams["article_id"], PDO::PARAM_INT);
            $stmt->bindParam('p_articletype', $arrParams["article_type"], PDO::PARAM_STR);
            $stmt->bindParam('p_limit', $arrParams["limit"], PDO::PARAM_INT);

            //Fetch result
            $stmt->execute();
            $arrResultTmp = $stmt->fetchAll();
            
            if(!empty($arrResultTmp)){
                foreach($arrResultTmp as $arrItem){
                    $arrResult[] = $arrItem['article_id'];
                }
            }

            //Close cursor
            $stmt->closeCursor();

            //Release
            unset($stmt);
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return false;
        }
        return $arrResult;
    }

    /**
     * Function get tags of artilce
     * @param int $intArticleID
     * @return array
     * @author  : nhanva@fpt.net
     */
    public function getTags($intArticleID)
    {
        $arrResult = array();
        try
        {
            $sport_m = Fpt_Global::getSportSlaveDB();
            $stmt = $sport_m->prepare('CALL sp_getObjectTagByArticleID(:p_articleid);');

            //Bind param
            $stmt->bindParam('p_articleid', $intArticleID, PDO::PARAM_INT);

            //Fetch result
            $stmt->execute();
            // Get result
            $arrResult = array();
            
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
            {
                $arrResult[] = $row['tag_id'];
            }
            
            //Close cursor
            $stmt->closeCursor();

            //Release
            unset($stmt);
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return false;
        }
        return $arrResult;
    }

    /**
     * Function get tags of artilce
     * @param string One or more tagid, seperate by comma
     * @return array
     * @author  : nhanva@fpt.net
     */
    public function getTagByID($strTagID)
    {
        $arrResult = array();
        try
        {

            $sport_m = Fpt_Global::getSportSlaveDB();
            $stmt = $sport_m->prepare('CALL sp_getDetailTags(:p_tagid);');

            //Bind param
            $stmt->bindParam('p_tagid', $strTagID, PDO::PARAM_STR);

            //Fetch result
            $stmt->execute();
            // Get result
            $arrResult = array();
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
            {
                $arrResult[$row['tag_id']] = $row;
            }
            // Close cursor
            $stmt->closeCursor();
            
            // Release variable
            unset ($stmt);

        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return false;
        }
        return $arrResult;
    }

    /**
     * Get article's detail by array ids
     * @param array $arrArticleIDs
     * @return array
     * @author QuangTM
     */
    public function getDetailArticleByIDs($arrArticleIDs)
    {
        try
        {
            $result = false;
            
            $strArticleIDs = implode(',', $arrArticleIDs);
            
            // Get DB Object
            $sport_s = Fpt_Global::getSportSlaveDB();
            
            // Prepare SQL
            $stmt = $sport_s->prepare('CALL sp_getDetailarticles(:p_article);');
            
            // Bind params
            $stmt->bindParam('p_article', $strArticleIDs, PDO::PARAM_STR);
            
            // Execute
            $stmt->execute();
            
            // Get result
            $result = array();
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
            {
                $result[$row['article_id']] = $row;
            }
            // Close cursor
            $stmt->closeCursor();
            
            // Release variable
            unset ($stmt, $strArticleIDs);
            
            // Return result
            return $result;
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }
	
	/**
     * Get list Article By Cate Public Time
     * @author Kietthm
     * @param <array> $arrPamram
     * @return <array> List article id
     */
    public function getArticleByCatePublicTime($arrPamram)
    {
        $return = array("data" => NULL, "total" => 0);
        try
        {
            
            // Get DB
            $sport_m = Fpt_Global::getSportDB();
            
            $stmt = $sport_m->prepare('CALL sp_getArticleByCatePublicTime(:p_categoryid,:p_fromdate, :p_todate, :p_limit,:p_offset,@p_rowcount )');

            //Bind param
            $stmt->bindParam('p_categoryid', $arrPamram["category_id"], PDO::PARAM_INT);
            $stmt->bindParam('p_fromdate', $arrPamram["fromdate"], PDO::PARAM_INT);
            $stmt->bindParam('p_todate', $arrPamram["todate"], PDO::PARAM_INT);
            $stmt->bindParam('p_limit', $arrPamram["limit"], PDO::PARAM_INT);
            $stmt->bindParam('p_offset', $arrPamram["offset"], PDO::PARAM_INT);
            
            //Fetch Result
            $stmt->execute();
            // Get result
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
            {
                $return['data'][] = $row['article_id'];
            }
            $stmt->closeCursor();

            $stmt = $sport_m->query("SELECT @p_rowcount");
            $p_intFoundRows = $stmt->fetchColumn();

            $return["total"] = $p_intFoundRows;
            //return $return;
        }
        catch (Zend_Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $return;
    }
    /**
     * Get list reference_id by article_id
     * @param int ($articleID,$limit,$offset)
     * @return array $arrResult
     * @author  : phongtx@fpt.net
     */
    public function getObjectReferenceByArticleID($articleID, $limit, $offset)
    {
        //echo '<pre>';print_r($articleID);exit;
        $arrResult = array();
        try
        {
            $sport_m = Fpt_Global::getSportDB();

            $stmt = $sport_m->prepare('CALL sp_getListObjectReference(:p_albumid, :p_limit, :p_offset, @p_rowcount)');

            //Bind param
            $stmt->bindParam('p_albumid', $articleID, PDO::PARAM_INT);
            $stmt->bindParam('p_limit', $limit);
            $stmt->bindParam('p_offset', $offset, PDO::PARAM_INT);
            
            $stmt->execute();
            // Get result
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
            {
                $arrResult['data'][$row['reference_id']] = $row;
            }
            // Close cursor
            $stmt->closeCursor();
            
            $stmt = $sport_m->query("SELECT @p_rowcount");
            
            $arrResult["total"] = intval($stmt->fetchColumn());
            //Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrResult;
    }
    /**
     * Get detail object reference by reference_id
     * @param int ($referenceID)
     * @return array $arrResult
     * @author  : phongtx@fpt.net
     */
    public function getObjectReferenceDetailByIDs($referenceID)
    {
        try
        {
            $result = false;
            
            $strReferenceIDs = implode(',', $referenceID);
            // Get DB Object
            $sport_s = Fpt_Global::getSportSlaveDB();
            // Prepare SQL
            $stmt = $sport_s->prepare('CALL sp_getDetailObjectReference(:p_referids)');
            
            // Bind params
            $stmt->bindParam('p_referids', $strReferenceIDs, PDO::PARAM_STR);
            
            // Execute
            $stmt->execute();
            
            // Get result
            $result = array();
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
            {
                $result[$row['reference_id']] = $row;
            }
            // Close cursor
            $stmt->closeCursor();
            // Release variable
            unset ($stmt, $referenceID);
        }
        catch (Zend_Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return false;
        }
        return $result;
    }
    /**
     * get list Article of 1 team
     * @author TriNT
     * @param <INT> $p_teamID
     * @param <INT> $p_limit
     * @return <Array>
     */
    public function getListArticleByTeam($p_teamID, $p_limit = 10, $intOffset = 0)
    {
        $arrResult = array();
        try
        {
            $sport_m = Fpt_Global::getSportDB();
            $stmt = $sport_m->prepare('CALL sp_getArticleTeam(:p_teamid, :p_offset, :p_limit, @p_row)');

            //Bind param
            $stmt->bindParam('p_teamid', $p_teamID, PDO::PARAM_INT);
            $stmt->bindParam('p_offset', $intOffset, PDO::PARAM_INT);
            $stmt->bindParam('p_limit', $p_limit, PDO::PARAM_INT);

            //Fetch result
            $stmt->execute();
            $arrResult = $stmt->fetchAll();

            //Close cursor
            $stmt->closeCursor();
            $stmt = $sport_m->query("SELECT @p_row");
            $total = $stmt->fetchColumn();
            $arrResult["data"] = $arrResult;
            $arrResult["total"] = $total;

            //Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return false;
        }
        return $arrResult;
    }
    /**
     * Get list article by hot
     * @param array $arrParams (category_id,article_type,limit,offset,site_id)
     * @return array $arrReturn
     * @author  : phongtx@fpt.net
     */
    public function getListArticleByHot($arrParams)
    {
        $arrResult = array();
        try
        {
            // Get DB
            $sport_m = Fpt_Global::getSportDB();
            $stmt = $sport_m->prepare('CALL sp_getHotNews(:p_categoryid, :p_articletype, :p_offset, :p_limit, @p_result)');

            //Bind param
            $stmt->bindParam('p_categoryid', $arrParams['category_id'], PDO::PARAM_INT);
            $stmt->bindParam('p_articletype', $arrParams['article_type'], PDO::PARAM_INT);
            $stmt->bindParam('p_offset', $arrParams['offset'], PDO::PARAM_INT);
            $stmt->bindParam('p_limit', $arrParams['limit'], PDO::PARAM_INT);
           
            // Fetch result
            $stmt->execute();
            $arrResult = $stmt->fetchAll();
            // Close cursor
            $stmt->closeCursor();
            
            $stmt = $sport_m->query("SELECT @p_result");
            $foundRows = $stmt->fetchColumn();

            //Return array
            $arrResult["data"]     = $arrResult;
            $arrResult["total"]    = $foundRows;
            

            // Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrResult;
    }

    /**
     * get hot news on cate: order by hot score
     * @param type $arrParams (category_id, article_type, offset, limit)
     * @return type 
     */
    public function getListArticleByHotOnCate($arrParams)
    {
        $arrResult = array();
        try
        { 
            // Get DB
            $sport_m = Fpt_Global::getSportDB();
            $stmt = $sport_m->prepare('CALL sp_getHotNewsOnCate(:p_cateids, :p_articletype, :p_offset, :p_limit, @p_total)');

            //Bind param
            $stmt->bindParam('p_cateids', $arrParams['category_id']);
            $stmt->bindParam('p_articletype', $arrParams['article_type']);
            $stmt->bindParam('p_offset', $arrParams['offset'], PDO::PARAM_INT);
            $stmt->bindParam('p_limit', $arrParams['limit'], PDO::PARAM_INT);
           
            // Fetch result
            $stmt->execute();
            $arrResult = $stmt->fetchAll();
            // Close cursor
            $stmt->closeCursor();
            
            $stmt = $sport_m->query("SELECT @p_total");
            $foundRows = $stmt->fetchColumn();

            //Return array
            $arrResult["data"]     = $arrResult;
            $arrResult["total"]    = $foundRows;
            

            // Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrResult;
    }
    /**
     * Get latest news on tag
     * @param array $arrParams array(TagID, Offset, Limit)
     * @return <array> List article id
     */
    public function getListArticleByTag($arrParams)
    {
        $arrResult = array();
        try
        {
            // Get DB
            $sport_m = Fpt_Global::getSportDB();
            $stmt = $sport_m->prepare('CALL sp_getArticleByTag(:p_tagid, :p_articletype, :p_offset, :p_limit, @p_rowcount)');

            //Bind param
            $stmt->bindParam('p_articletype', $arrParams['ArticleType']);
            $stmt->bindParam('p_tagid', $arrParams['TagID'], PDO::PARAM_INT);
            $stmt->bindParam('p_offset', $arrParams['Offset'], PDO::PARAM_INT);
            $stmt->bindParam('p_limit', $arrParams['Limit'], PDO::PARAM_INT);

            // Fetch result
            $stmt->execute();
            $arrResult = $stmt->fetchAll();
            
            // Close cursor
            $stmt->closeCursor();
			
            $stmt = $sport_m->query("SELECT @p_rowcount");
            $foundRows = $stmt->fetchColumn();
			
            //Return array
            $arrResult["data"]     = $arrResult;
            $arrResult["total"]    = $foundRows;


            // Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrResult;
    }
    
    public function getDetailTopic($strTopicID)
    {
        $arrResult = array();
        try
        {
            // Get DB
            $sport_m = Fpt_Global::getSportDB();
            $stmt = $sport_m->prepare('CALL sp_getDetailTopic(:p_topicid)');

            //Bind param
            $stmt->bindParam('p_topicid', $strTopicID, PDO::PARAM_STR);
            
            // Fetch result
            $stmt->execute();
            $arrResult = $stmt->fetchAll();
            // Close cursor
            $stmt->closeCursor();

            // Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrResult;
    }
    /**
     * Get Page Extend
     * @param int $intArticleID
     * @param int $intPageOrder
     * @return array
     * @author  : TriNT
     */
    public function getDetailPageExtend($intArticleID, $intPageOrder )
    {
        $arrResult = array();
        try
        {
            $sport_m = Fpt_Global::getSportDB();

            $stmt = $sport_m->prepare('CALL sp_getDetailPageExtend(:p_article, :p_pageorder)');

            //Bind param
            $stmt->bindParam('p_article', $intArticleID, PDO::PARAM_STR);
            $stmt->bindParam('p_pageorder', $intPageOrder, PDO::PARAM_STR);

            //Fetch result
            $stmt->execute();
            $arrResult = $stmt->fetch();

            //Close cursor
            $stmt->closeCursor();

            //Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return false;
        }
        return $arrResult;
    }
    
    public function getListArticleByRangeID($minID, $maxID)
    {
        $arrResult = array();
        try
        {
            $sport_m = Fpt_Global::getSportDB();

            $stmt = $sport_m->prepare('SELECT article_id FROM articles WHERE article_id > '.$minID.' AND article_id < '.$maxID);

            //Fetch result
            $stmt->execute();
            
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
            {
                $arrResult[] = $row['article_id'];
            }

            //Close cursor
            $stmt->closeCursor();

            //Release
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return false;
        }
        return $arrResult;
    }
    
    public function getRangeArticle()
    {
        $arrResult  = array('max_id' => 0);
        try
        {
            //Get DB
            $sport_m = Fpt_Global::getSportDB();
            $stmt = $sport_m->prepare('CALL sp_getRangeArticles()');

            //Bind param
            
            // Fetch result
            $stmt->execute();
            $arrResult = $stmt->fetch();
           
            // Close cursor
            $stmt->closeCursor();
            
            unset($stmt);
        }
        catch (Zend_Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $arrResult;
    }
}